import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '../layout/Layout.vue'
import Home from '../views/home/Home.vue'
import Login from '../views/Login.vue'
import Student from '../views/student/Student.vue'
import ClassInfo from '../views/ClassInfo/ClassInfo.vue'
import Finance from '../views/Finance/Finance.vue'
import Sysmbol from '../views/sysmbol/Sysmbol.vue'
import Teacher from '../views/teacher/Teacher.vue'
import Order from '../views/order/order.vue'
import Regiester from '../views/Regiester.vue'
Vue.use(VueRouter)

<<<<<<< HEAD

const routes = [

    {
        path: '/',
        name: Layout,
        component: Layout,
        redirect: '/home',
        children: [{
                path: 'home',
                name: Home,
                component: Home,
                rules: ['管理员', '普通用户'],
                meta: { title: '首页中心', }
            },
            {
                path: 'student',
                name: Student,
                component: Student,
                rules: ['管理员'],
                meta: { title: '学员管理', },
                icon: ''

            },
            {
                path: 'classInfo',
                name: ClassInfo,
                component: ClassInfo,
                rules: ['管理员', '普通用户'],
                meta: { title: '课程管理', }

            },
            {
                path: 'finance',
                name: Finance,
                component: Finance,
                rules: ['管理员'],
                meta: { title: '财务管理', }
            },

            {
                path: 'teacher',
                name: Teacher,
                component: Teacher,
                rules: ['管理员'],
                meta: { title: '教师管理', }

            },
            {
                path: 'order',
                name: Order,
                component: Order,
                rules: ['管理员', '普通用户'],
                meta: { title: '订单管理', }

            },
            {
                path: 'sysmbol',
                name: Sysmbol,
                component: Sysmbol,
                rules: ['管理员', '普通用户'],
                meta: { title: '系统管理', }
            },
        ]
    },
    {
        path: '/login',
        name: Login,
        component: Login,
    }
=======
const routes = [{
  path: '/',
  name: Layout,
  component: Layout,
  redirect: '/login',
  children: [{
    path: 'home',
    name: Home,
    component: Home
  },
  {
    path: 'student',
    name: Student,
    component: Student
  },
  {
    path: 'classInfo',
    name: ClassInfo,
    component: ClassInfo
  },
  {
    path: 'finance',
    name: Finance,
    component: Finance
  },
  {
    path: 'sysmbol',
    name: Sysmbol,
    component: Sysmbol
  },
  {
    path: 'teacher',
    name: Teacher,
    component: Teacher
  },
  {
    path: 'order',
    name: Order,
    component: Order
  }
  ]
},
{
  path: '/login',
  name: Login,
  component: Login,
},
{
  path: '/regiester',
  name: Regiester,
  component: Regiester,
}
>>>>>>> f7cfb8c8c0dc04e038c83cd03fa5d6b0879aa247
]

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};

const router = new VueRouter({
    routes
})

import { getLoginDataApi } from '../api/loginApi'
import store from '../store/index'
import { getToken } from '../mock/cookieUtils'

var whileArr = ['/login', '/regiester']; //白名单 , 这里面所有的路由都不需要登录就能访问

//登录权限功能
router.beforeEach((to, from, next) => {
    var path = to.path; //当前路由
    var token = getToken("token");
    console.log(token)
    if (token) {
        if (path == '/login') { //判断是不是登录页面
            next('/home') //是 , 进入首页
        } else {
            //不是 请求拿到用户基本信息 , 制作权限路由 , 生成菜单

            //判断如果vuex已经保存了routes, 我们不用重发请求
            if (store.state.routes.length > 0) {
                console.log(store.state.routes)
                next()

            } else {
                //发送请求拿基本信息 , 保存vuex
                getLoginDataApi({ token: token }).then(res => {
                    if (res.code == 200) {
                        console.log(res.data)

                        store.commit('setUser', res.data); //向vuex里面保存 登录用户信息
                        console.log(store.state.user)
                        var routesData = routes[0].children.filter(item => {
                            return item.rules.indexOf(res.data.rules) != -1;
                        })
                        console.log(routesData)
                        store.commit('setRoutes', routesData)
                        console.log(store.state.routes)
                        router.addRoutes(routes) //动态刷新路由
                        next({...to, replace: true }) //保证addRoutes刷新路由完整

                    }
                })
            }

        }
    } else {
        //token为空 没有登录
        if (whileArr.includes(path)) {
            //在白名单放行
            next()
        } else {
            //没在白名单
            next('/login') //没在白名单 , 也没有token去登录
        }

    }
    next()
})

export default router